package com.smec.moblie.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.smec.moblie.common.ConstValue;
import com.smec.moblie.control.MainControl;
import com.smec.moblie.control.PublicControl;
import com.smec.moblie.location.PollingUtils;
import com.smec.moblie.location.SmecMntMapService;

import me.imid.swipebacklayout.lib.SwipeBackLayout;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, ConstValue {
    /**
     * 提示
     **/
    private SVProgressHUD pd = null;
    /**
     * 本地照片地址
     */
    protected String imgPath;

    /**
     * 滑动结束activity动画
     **/
    private SwipeBackLayout mSwipeBackLayout;
    //图片加载
    protected ImageLoader imageLoader = ImageLoader.getInstance();

    public static Boolean isShowVersionDialog = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐去标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.remes_act_base);

//        setStatusBarLightMode(this, android.R.color.transparent);
//        setStatusBarLightMode(this, Color.parseColor("#009990"));
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//        }
//        Eyes.translucentStatusBar(BaseActivity.this, true);

        imgPath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
//        mSwipeBackLayout = getSwipeBackLayout();
        //设置滑动方向，可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
//        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        // 开始定位服务
        PollingUtils.startPollingService(this, 60, SmecMntMapService.class, SmecMntMapService.ACTION, "SmecMntMapService");
        setContentView();
//        Eyes.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimary));
        initView();
        getData();
        if (pd == null) {
            pd = new SVProgressHUD(this);
        }
        if (!isShowVersionDialog) {
            checkIsOlderVersion();
        }
    }

    protected abstract void setContentView();

    protected abstract void initView();

    protected abstract void getData();

    public void showSVProgressHUD() {
        if (!pd.isShowing()) {
            pd.showWithStatus("正在登入...");
        }
    }

    public void stopSVProgressHUD() {
        if (pd.isShowing()) {
            pd.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        try {
            mSwipeBackLayout.scrollToFinishActivity();
        } catch (Exception e) {

        }

        super.onDestroy();
    }

    // 检查是否为最新版本
    private void checkIsOlderVersion() {

        new AsyncTask<Void, Void, Void>() {
            String retString;

            @Override
            protected void onPostExecute(Void result) {
                String currVersion = PublicControl.getAppVersionName(BaseActivity.this);
                if (retString == null) {
                    Log.e("", "无法获取app版本号，请检查");
                } else {

                    // 小版本更新提示，大版本更新强制更新
                    try {
                        String[] retStrings = retString.split("\\.");
                        String[] currVersionStrings = currVersion.split("\\.");
                        int ret1 = Integer.parseInt(retStrings[0]);
                        int ret2 = Integer.parseInt(retStrings[1]);
                        int ret3 = Integer.parseInt(retStrings[2]);
                        int currVersion1 = Integer.parseInt(currVersionStrings[0]);
                        int currVersion2 = Integer.parseInt(currVersionStrings[1]);
                        int currVersion3 = Integer.parseInt(currVersionStrings[2]);

                        if (ret1 > currVersion1 || ret2 > currVersion2) {
                            new AlertView("提示", "有重要更新版本发布，此版本为强制更新,请到mobile@work中更新，最新版本号为:v" + retString, null, new String[]{"升级"}, null, BaseActivity.this,
                                    AlertView.Style.Alert, new OnItemClickListener() {

                                @Override
                                public void onItemClick(Object o, int position) {

                                    Intent resolveIntent = getPackageManager().getLaunchIntentForPackage("com.mobileiron");// 这里的packname就是从上面得到的目标apk的包名
                                    // 启动目标应用
                                    startActivity(resolveIntent);
                                    BaseActivity.this.finish();
                                }
                            }).setCancelable(false).show();
                        } else if (ret3 > currVersion3) {

                            new AlertView("提示", "当前不是最新版本,请到mobile@work中更新，最新版本号为:v" + retString, "取消", new String[]{"升级"}, null, BaseActivity.this,
                                    AlertView.Style.Alert, new OnItemClickListener() {

                                @Override
                                public void onItemClick(Object o, int position) {
                                    if (position == 0) {
                                        Intent resolveIntent = getPackageManager().getLaunchIntentForPackage("com.mobileiron");// 这里的packname就是从上面得到的目标apk的包名
                                        // 启动目标应用
                                        startActivity(resolveIntent);
                                        BaseActivity.this.finish();
                                    }
                                }
                            }).show();

                            isShowVersionDialog = true;
                        }

                    } catch (Exception e) {
                        Log.e("err", e.getMessage());
                    }
                }
                super.onPostExecute(result);
            }

            @Override
            protected Void doInBackground(Void... params) {
                retString = MainControl.getVersion();
                Log.i("", "==服务器版本:" + retString);

                return null;
            }

        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }
}
